//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "PaymentTypes.h"
#include "FormUtilities.h"
#include "PaymentTypeDlg.h"


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
__fastcall TfrmPaymentTypes::TfrmPaymentTypes(TComponent* Owner, TDatabase * dbMain)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->dbMain = dbMain; 
   paymentTypes = new TStringList();
   paymentTypes->Duplicates = dupAccept;
   bridge = new TPaymentTypeBridge( dbMain);
}

void TfrmPaymentTypes::SetButtonsState( int row)
{
   bool enabled;

   enabled = ( paymentTypes->Count > 0);
   
   btnUpdate->Enabled = enabled;
   btnDelete->Enabled = enabled;
}

void TfrmPaymentTypes::RefreshGrid()
{
   TPaymentType* pt;
   int rowCount;
   int i;

   rowCount = 2;
   bridge->GetPaymentTypes( paymentTypes);

   if ( paymentTypes->Count > 1)
      rowCount = paymentTypes->Count + 1;

   grdPaymentTypes->RowCount = rowCount;

   for ( i = 1; i < grdPaymentTypes->RowCount; i++)
      grdPaymentTypes->Rows[ i]->Clear();

   for ( i = 0; i < paymentTypes->Count; i++)
   {
      pt = ( TPaymentType*)paymentTypes->Objects[ i];

      grdPaymentTypes->Cells[ 0][ i + 1] = pt->description;
   }//for

   TFormUtilities::SetLastColumnWidth( grdPaymentTypes);

   SetButtonsState( grdPaymentTypes->Row);
}


//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::FormShow(TObject *Sender)
{
   grdPaymentTypes->Cells[ 0][ 0] = "DESCRIPTION";

   RefreshGrid();
}
//---------------------------------------------------------------------------


void __fastcall TfrmPaymentTypes::btnAddClick(TObject *Sender)
{
   TfrmPaymentTypeDlg * frmPaymentTypeDlg;

   frmPaymentTypeDlg = new TfrmPaymentTypeDlg( this, bridge, NULL);

   try
   {
      if ( frmPaymentTypeDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmPaymentTypeDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::FormDestroy(TObject *Sender)
{
   int i;
   
   delete bridge;
   
   TStringUtilites :: Clear( paymentTypes);
   delete paymentTypes;
}
//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::btnDeleteClick(TObject *Sender)
{
   TPaymentType* pt;

   pt = ( TPaymentType*)paymentTypes->Objects[ grdPaymentTypes->Row - 1];

   if ( bridge->InUse( pt))
   {
      MessageDlg( "Payment type in use. Unable to delete.",
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);
   }
   else if ( MessageDlg( "Are you sure ?",
                    mtWarning,
                    TMsgDlgButtons() << mbYes << mbNo,
                    0) == mrYes)
   {
        bridge->Delete( pt->id);
        delete pt;
        paymentTypes->Delete( grdPaymentTypes->Row - 1);
        RefreshGrid();
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::grdPaymentTypesSelectCell(TObject *Sender,
      int ACol, int ARow, bool &CanSelect)
{
   SetButtonsState( ARow);        
}
//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::btnUpdateClick(TObject *Sender)
{
   TfrmPaymentTypeDlg * frmPaymentTypeDlg;
   TPaymentType * pt;

   pt = ( TPaymentType*)paymentTypes->Objects[ grdPaymentTypes->Row - 1];
   frmPaymentTypeDlg = new TfrmPaymentTypeDlg( this, bridge, pt);

   try
   {
      if ( frmPaymentTypeDlg->ShowModal() == mrOk)
         RefreshGrid();
   }
   __finally
   {
      delete frmPaymentTypeDlg;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::grdPaymentTypesKeyDown(TObject *Sender,
      WORD &Key, TShiftState Shift)
{
   switch ( Key)
   {
      case VK_INSERT :
         btnAddClick( Sender);
         break;

      case VK_DELETE :
         if ( btnDelete->Enabled)
            btnDeleteClick( Sender);
         break;

      case VK_SPACE :
         if ( btnUpdate->Enabled)
            btnUpdateClick( Sender);
         break;
   }
}
//---------------------------------------------------------------------------

void __fastcall TfrmPaymentTypes::btnCloseClick(TObject *Sender)
{
   Close();        
}
//---------------------------------------------------------------------------



